#!/bin/sh
###############################################################################
##  Author    : xuezhoyi
##  Name      : edw_data_exporter.sh
##  Functions : 
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
##  2.0      2018-01-24  mayanting     
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh

function USAGE(){
    echo -e "\nHow to use this shell script!"
    echo -e '\n${V_SHELL_HOME}/edw_data_exporter.sh -m mysql -t EDW.CM_GUARANTY_GOODS -f /home/hadoop/dev/data/EDW.CM_GUARANTY_GOODS.TXT'
    echo -e '\n${V_SHELL_HOME}/edw_data_exporter.sh -m hive -t EDW.CM_GUARANTY_GOODS -f /home/hadoop/dev/data/EDW.CM_GUARANTY_GOODS.TXT'
}

# Get the parameter
while getopts :m:t:f: args;do
    case ${args} in
        m)
            V_MODE=${OPTARG}
        ;;
        t)
            V_TABLE=${OPTARG}
        ;;
        f)
            V_FILE=${OPTARG}
        ;;
        ?)
            USAGE
        ;;
    esac
done

# Ensure the basic variavle of connection is not null
if [[ -z ${V_MODE} || -z ${V_TABLE} || -z ${V_FILE} ]];then
    USAGE
    exit 3
fi

# Get data date
V_DATA_DATE=$(grep V_DATA_DATE "${V_SHELL_PARM}/ALL_ORG/edw_parm.txt" | awk -F'=' '{print $2}')

# 
case ${V_MODE} in
    mysql)
        # Get table column
        V_QUERY="SELECT GROUP_CONCAT(B.COLUMN_NAME) FROM ETL.EDW_DATA_SYNC A INNER JOIN INFORMATION_SCHEMA.COLUMNS B ON UPPER(A.TABLE_NAME)=UPPER(CONCAT(B.TABLE_SCHEMA, '.', B.TABLE_NAME)) WHERE A.TABLE_NAME = '${V_TABLE}' AND A.SYNC_TYPE = 'M2H' GROUP BY A.TABLE_NAME ORDER BY B.ORDINAL_POSITION;"
        V_TAB_COL=$(${V_RUN_MYSQL} -q "${V_QUERY}")
        # Get where condition
        V_QUERY="SELECT COALESCE(WHERE_CONDITION,'') FROM ETL.EDW_DATA_SYNC WHERE TABLE_NAME = '${V_TABLE}' AND SYNC_TYPE = 'M2H';"
        V_WHERE=$(${V_RUN_MYSQL} -q "${V_QUERY}")
        if [[ ${V_WHERE} == '' ]];then
            V_QUERY="SELECT ${V_TAB_COL} FROM ${V_TABLE};"
        else #???
            V_QUERY=$(eval echo '$'"XAVIER SELECT ${V_TAB_COL} FROM ${V_TABLE} WHERE ${V_WHERE};")
        fi
        ${V_RUN_MYSQL} -x "${V_QUERY}" | sed s/\\t/${V_DELIMITER}/g > ${V_FILE}
        if [[ $? -eq 0 ]];then
            echo -e "Export data to:${V_FILE}"
        else
            echo -e "ERROR:Fail to export table[${V_TABLE}]."
            exit 1
        fi
    ;;
    hive)
        # Get table column
        V_QUERY="SELECT GROUP_CONCAT(B.COLUMN_NAME) FROM ETL.EDW_DATA_SYNC A INNER JOIN INFORMATION_SCHEMA.COLUMNS B ON UPPER(A.TABLE_NAME)=UPPER(CONCAT(B.TABLE_SCHEMA, '.', B.TABLE_NAME)) WHERE A.TABLE_NAME = '${V_TABLE}' AND A.SYNC_TYPE = 'H2M' GROUP BY A.TABLE_NAME ORDER BY B.ORDINAL_POSITION;"
        V_TAB_COL=$(${V_RUN_MYSQL} -q "${V_QUERY}")
        # Get where condition
        V_QUERY="SELECT COALESCE(WHERE_CONDITION,'') FROM ETL.EDW_DATA_SYNC WHERE TABLE_NAME = '${V_TABLE}' AND SYNC_TYPE = 'H2M';"
        V_WHERE=$(${V_RUN_MYSQL} -q "${V_QUERY}")
        if [[ ${V_WHERE} == '' ]];then
            V_QUERY="SELECT ${V_TAB_COL} FROM ${V_TABLE};"
        else
            V_QUERY=$(eval echo '$'"XAVIER SELECT ${V_TAB_COL} FROM ${V_TABLE} WHERE ${V_WHERE};")
        fi
        ${V_RUN_HIVQL} -x "${V_QUERY}" | sed s/\\t/${V_DELIMITER}/g > ${V_FILE}
        if [[ $? -eq 0 ]];then
            echo -e "Export data to:${V_FILE}"
        else
            echo -e "ERROR:Fail to export table[${V_TABLE}]."
            exit 1
        fi
    ;;
esac

exit 0
